
import Debug from "../../tools/Debug";
import { KeyMatchInfo, SELF_KEY_CODE } from "./InputConfig";
/**
 * 键值转换器
 */
export default class InputConverter {

    /**
     * platform 平台名称
     * keyCode 平台键值
     * value  自定义的按键信息KeyMatchInfo
     */
    private keyMapGroup: { [platform: string]: { [group: number]: { [keyCode: number]: KeyMatchInfo } } } = {}

    //通过自定义键值获得信息
    private keyCodeInfo: { [group: number]: { [keyCode: number]: KeyMatchInfo } } = {}
    //通过自定义键名获得信息
    private keyNameInfo: { [group: number]: { [keyName: string]: KeyMatchInfo } } = {}


    //完整的配置数据
    protected config: KeyMatchInfo[] = null;

    //不同操作方式对应的映射表
    protected keyCodeMap: { [key: string]: any } = {}

    protected _group: number = 1;

    protected _curGroup: number = 1;

    set curGroup(g: number) {
        this._curGroup = g;
    }

    get curGroup() {
        return this._curGroup;
    }

    setGroup(g: number = 1) {
        this._group = g;
        Debug.log('InputConverter group is ', this._group)
    }

    getGroup() {
        return this._group;
    }

    getDefaultGroup() {
        return 1;
    }

    /**
     * 通过自定义键值获得自定义键名。
     * @param keyCode 自定义键值
     * @returns 
     */
    getKeyName(keyCode: number, group?: number) {
        let data = this.getKeyCodeInfo(keyCode, group)
        return data?.self
    }

    getKeyCodeInfo(keyCode: number, group?: number) {
        group = group || this.getGroup();
        let data = this.keyCodeInfo[group][keyCode]
        if (!data) {
            data = this.keyCodeInfo[this.getDefaultGroup()][keyCode]
        }
        return data;
    }

    getKeyNameInfo(keyName: string, group?: number) {
        group = group || this.getGroup();
        let groupData = this.keyNameInfo[group];
        if (groupData) {
            let data = this.keyNameInfo[group][keyName]
            if (!data) {
                data = this.keyNameInfo[this.getDefaultGroup()][keyName]
            }
            return data;
        } else {
            Debug.warn('getKeyNameInfo group ', group)
        }



    }


    getKeyImg(keyName: string, imgKey: string, group?: number) {
        let data = this.getKeyNameInfo(keyName, group);
        if (data) {
            return data[imgKey]
        } else {
            return ''
        }
    }
    /**
     * 添加一个平台的键值表
     * @param platform 平台名称 如 cc  steam
     * @param KEY_CODE 按键与键值的对应数据
     */
    addKeyCode(platform: string, KEY_CODE: any) {
        this.keyCodeMap[platform] = KEY_CODE;
        this.setData(platform)
    }

    setData(platform: string) {
        let KEY_CODE: any = this.keyCodeMap[platform]
        Debug.log('KEY_CODE  ', KEY_CODE, ' platform ', platform)
        if (this.config && !this.keyMapGroup[platform] && KEY_CODE) {
            this.keyMapGroup[platform] = {}
            for (const index in this.config) {
                const element: KeyMatchInfo = this.config[index];
                let groupNum = element.group;
                let groupData = this.keyMapGroup[platform][groupNum]
                if (!groupData) {
                    groupData = {}
                    this.keyMapGroup[platform][groupNum] = groupData;
                }

                let keyCodeInfo = this.keyCodeInfo[groupNum]
                if (!keyCodeInfo) {
                    keyCodeInfo = {}
                    this.keyCodeInfo[groupNum] = keyCodeInfo
                }
                keyCodeInfo[this.getKeyValueByKeyName(element.self)] = element;
                let keyNameInfo = this.keyNameInfo[groupNum]
                if (!keyNameInfo) {
                    keyNameInfo = {}
                    this.keyNameInfo[groupNum] = keyNameInfo
                }
                keyNameInfo[element.self] = element;

                let list: string[] = element[platform];
                for (let k = 0; k < list.length; k++) {
                    let keyName = list[k];//平台按键名称
                    // let selfName = element.self;
                    const keyCode = KEY_CODE[keyName];//对应平台的键值。
                    if (keyCode == undefined) {
                        Debug.warn('键值不存在 ', keyName, ' groupNum ', groupNum, ' platform ', platform, keyCode)
                    }
                    // console.log("InputConverter platform  ", platform, key, element.self)
                    // let keyNameInfo = this.keyNameInfo[groupNum]
                    // if (groupData[keyCode] && keyNameInfo[selfName]) {
                    //     LogUtils.warn('键值重复:', keyCode, list[k])
                    // }
                    groupData[keyCode] = element;

                    // if (groupNum != this.getDefaultGroup()) {
                    //     let defaultGroup = this.keyMapGroup[platform][this.getDefaultGroup()]
                    //     let keyNameInfo = this.keyNameInfo[this.getDefaultGroup()]
                    //     if (defaultGroup && keyNameInfo && defaultGroup[keyCode] && !keyNameInfo[selfName]) {
                    //         LogUtils.warn('与默认键值重复:', keyCode, list[k])
                    //     }
                    // }

                }

            }
            Debug.log(' keyNameInfo ', this.keyNameInfo)
            Debug.log(' keyCodeInfo ', this.keyCodeInfo)
            Debug.log(' keyMapGroup ', this.keyMapGroup)
        }
    }

    init(config: KeyMatchInfo[]) {
        this.config = config;

        for (const platform in this.keyCodeMap) {
            this.setData(platform)
        }
    }

    // protected setKeyCodeConfig(platform: string) {
    //     let KEY_CODE: any = this.keyCodeMap[platform]
    //     if (this.config && !this.keyMap[platform] && KEY_CODE) {
    //         this.keyMap[platform] = {}
    //         for (const index in this.config) {
    //             const element = this.config[index];
    //             this.selfValueKey[this.getKeyValueByKeyName(element.self)] = element.self;
    //             let list = element[platform];
    //             for (let k = 0; k < list.length; k++) {
    //                 const key = KEY_CODE[list[k]];

    //                 // console.log("InputConverter platform  ", platform, key, element.self)
    //                 this.keyMap[platform][key] = element.self;
    //   this.data[element.self] = element;
    //             }
    //         }
    //     }
    // }

    getKeyMapGroupInfo(platform: string, keyCode: number): KeyMatchInfo {
        if (!this.keyMapGroup[platform]) {
            console.log('getKeyValue data is null ', platform, keyCode)
            return null;
        }
        let group = this.getGroup();
        let data = this.keyMapGroup[platform][group]

        if (data) {
            let id: KeyMatchInfo = data[keyCode]
            if (!id) {
                data = this.keyMapGroup[platform][this.getDefaultGroup()]
                id = data[keyCode]
            }
            return id;
        }
        return null;
    }

    /**
     * 键值映射，通过平台的key，或者使用的key
     * @param platform 平台名称
     * @param keyCode  不同平台的键值
     * @returns 自定义键值
     */
    getKeyValue(platform: string, keyCode: number) {
        let info = this.getKeyMapGroupInfo(platform, keyCode)
        if (info) {
            let value = this.getKeyValueByKeyName(info.self)
            if (!value) {
                console.warn(' value is null ', keyCode, platform, ' info.self ', info.self, ' group ', this.getGroup())
            }
            return value;
        }
        return 0;
        // if (!this.keyMapGroup[platform]) {
        //     console.warn('getKeyValue data is null ', platform, keyCode)
        //     return 0;
        // }
        // let group = this.getGroup();
        // let data = this.keyMapGroup[platform][group]

        // if (data) {
        //     let id = data[keyCode]
        //     if (!id) {
        //         data = this.keyMapGroup[platform][1]
        //         id = data[keyCode]
        //     }
        //     let value = this.getKeyValueByKeyName(id.self)
        //     if (!value) {
        //         console.warn(' value is null ', keyCode, platform, ' id ', id, ' group ', group)
        //     }
        //     return value;
        // } else {
        //     console.warn(' data is null ', platform, keyCode)
        // }
        // return 0;
    }

    getKeyValueByKeyName(name: string) {
        return SELF_KEY_CODE[name];
    }
}

